package com.webull.information.center.carwler.job.splan.frame;

import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.information.center.carwler.common.model.StockTitle;
import com.webull.securities.client.cache.model.TickerView;
import com.webull.securities.client.service.ExchangeService;
import com.webull.securities.client.service.TickerService;

@Component
public class SplanConverter {

	private @Autowired TickerService tickerService;
	private @Autowired ExchangeService exchangeService;
	
	/**
	 * 单标的 google转换器<br>
	 * sinaCn转换器<br>
	 * 
	 * @param tk
	 * @return
	 */
	public StockTitle googleConver1(SplanTask tk) {
		return getStockTitle(Integer.valueOf(tk.getTarget()));
	}

	public StockTitle cnbcConver1(SplanTask tk) {
		StockTitle stockTitle = getStockTitle(Integer.valueOf(tk.getTarget()));
		if ("2".equals(tk.getTargetExt())) {
			stockTitle.setCfgSource("cnbc_en");
		} else {
			stockTitle.setCfgSource("cnbc_etf");
		}
		return stockTitle;
	}

	public StockTitle bloombConver1(SplanTask tk) {
		StockTitle stockTitle = getStockTitle(Integer.valueOf(tk.getTarget()));
		if (StringUtils.isNotEmpty(tk.getTargetExt())) {
			stockTitle.setBbRegionAlias(tk.getTargetExt());
			return stockTitle;
		}
		return null;
	}

	private StockTitle getStockTitle(Integer tickerId) {
		TickerView tickerView = tickerService.getByTickerId(tickerId);
		if (tickerView != null) {
			StockTitle stockTitle = new StockTitle();
			BeanUtils.copyProperties(tickerView, stockTitle);
			Optional.ofNullable(stockTitle.getExchangeId())//
					.map(c -> exchangeService.getById(c))//
					.ifPresent(v -> {
						stockTitle.setExchangeAlias(v.getAlias());
						stockTitle.setExchangeCode(v.getExchangeCode());
					});
			return stockTitle;
		}
		return null;
	}


}
